package SQL.valueUtil;

import java.util.ArrayList;
import Common.Types;
import heap.ResultRow;
import heap.ResultSchema;
import SQL.abysnSQL.BoolCondition;
import SQL.abysnSQL.ConstValue;
import SQL.abysnSQL.FloatValue;
import SQL.abysnSQL.HvCondition;
import SQL.abysnSQL.HvValue;
import SQL.abysnSQL.IntegerValue;
import SQL.abysnSQL.StringValue;
import SQL.abysnSQL.Value;
import SQL.query.Select;
/**
 * 
 * @author Azure
 *
 */
public class SQLDeal {
	public SQLDeal(Select s,ResultRow tuple,ResultSchema schema){
		this.select = s;
		this.tuple = tuple;
		this.schema = schema;
	}
	/**
	 * get the SQLdeal result(transfered the select sql)
	 * @return
	 */
	public Select getResult(){
		deal(select);
		return select;
	}
	/**
	 * deal select s(transfer where & hvCondition)
	 * @param s
	 */
	public void deal(Select s) {
		if(s.where!=null)
			deal(s.where);
		if(s.hving!=null)
			deal(s.hving);
		// TODO Auto-generated method stub
		
	}
	/**
	 * deal hvCondition(as hving)
	 * @param hving
	 */
	public void deal(HvCondition hving) {
		switch (hving.type) {
		case AND:
		case OR:
			deal(hving.leftC);
			deal(hving.rightC);
			break;
		case BOOL:
			deal(hving.bool);
			break;
		case COP:
			deal(hving.left);
			deal(hving.right);
		}
		
	}
	/**
	 * deal hvValue
	 * @param hvV
	 */
	public void deal(HvValue hvV) {
		switch(hvV.type){
		case FUNC:
			//TODO
		case VALUE:
			deal(hvV.value);
		}
		
	}
	/**
	 * deal Value
	 * @param value
	 */
	public void deal(Value value) {
		// TODO Auto-generated method stub//COLNAME, CONST, AOPEXP, SUBQUERY, VALUE
		switch(value.type){
		case AOPEXPR:
			deal(value.l);
			deal(value.r);
			break;
		case SUBQUERY:
			deal((Select)value.subquery);
			break;
		case VALUE:
			deal(value.value);
			break;
		case COLNAME:
			//if the colname is exist..return as a string constValue
			//add the new colname to Value list
			ConstValue constvalue = deal(value.colname.toString());
			if(constvalue != null){
				value.type = Value.TYPE.CONST;
				value.constvalue = constvalue;
				this.v.add(value);
			}
		}
	}
	/**
	 * deal boolcondition(where~)
	 * @param where
	 */
	public void deal(BoolCondition where) {
		
	}
	/**
	 * deal string
	 * @param s
	 * @return
	 */
	public ConstValue deal(String s){
		ConstValue result = null;
		//check the schema~
		for (int i = 0; i < schema.size(); i++) {
			if (schema.get(i).name.equals(s) ||
					schema.get(i).name.endsWith("."+s)) {
				switch (schema.get(i).type) {
				case Types.INT:
				case Types.BOOLEAN:
					result = new IntegerValue(Integer.parseInt(tuple.get(i)));
					break;
				case Types.FLOAT:
					result = new FloatValue(Float.parseFloat(tuple.get(i)));
					break;
				default:
					result = new StringValue(tuple.get(i));
				}
				break;
			}
		}
		return result;
	}
	
	public Select select;
	public ResultRow tuple;
	public ResultSchema schema;
	public ArrayList<Value> v = new ArrayList<Value>();
}
